package com.bourse.core.services.cotation;

import java.util.NoSuchElementException;

import org.joda.time.DateMidnight;

import com.bourse.core.utils.TimeSerie;
import com.bourse.core.utils.TimeSerieObject;


public class CotationDayDate {
		
	DateMidnight firstDate;
	
	DateMidnight currentDate;
	
	DateMidnight lastDate;

	/**
	 * Constructor
	 * @param dateMidnight
	 * @param finalDate
	 */
	public CotationDayDate(DateMidnight dateMidnight, DateMidnight finalDate, boolean asc){
		this.firstDate = dateMidnight;
		this.currentDate = asc == true ? dateMidnight : finalDate;
		this.lastDate = finalDate;
	}
	
	public CotationDayDate(TimeSerie timeSerie, boolean asc){
		this.firstDate = timeSerie.getFirstDate();
		this.currentDate = asc == true ? timeSerie.getFirstDate() : timeSerie.getLastDate();
		this.lastDate = timeSerie.getLastDate();
	}
	
	public CotationDayDate(TimeSerieObject<?> timeSerieObject, boolean asc){
		this.firstDate = timeSerieObject.getFirstDate();
		this.currentDate = asc == true ? timeSerieObject.getFirstDate() : timeSerieObject.getLastDate();
		this.lastDate = timeSerieObject.getLastDate();
	}
	
	/**
	 * Return the previous date.
	 * @return
	 */
	public DateMidnight previous(){		
		DateMidnight date = currentDate.minusDays(1);
		int day = date.getDayOfWeek();
		currentDate = date;
		
		if(date.isBefore(firstDate)){
			throw new NoSuchElementException();
		}
		
		if(day != 6 && day != 7){
			return date;
		}else{
			return previous();
		}
	}
	
	/**
	 * Return the nextDate	
	 * @return
	 */
	public DateMidnight next(){
		DateMidnight date = currentDate.plusDays(1);
		int day = date.getDayOfWeek();
		currentDate = date;
		
		if(date.isAfter(lastDate) ){
			throw new NoSuchElementException();
		}
		
		if(day != 6 && day != 7){
			return date;		
		}else{
			return next();
		}
	}
	
	/**
	 * Return the current date
	 * @return
	 */
	public DateMidnight current(){
		return currentDate;
	}
	
	/**
	 * See if there is still previous date.
	 * @return
	 */
	public boolean hasPrevious(){
		if(currentDate.equals(firstDate)){
			return false;
		}else{
			return true;
		}
	}
	
	/**
	 * See if there is next dates.
	 * @return
	 */
	public boolean hasNext(){
		if(currentDate.equals(lastDate)){
			return false;
		}else{
			return true;
		}
	}
	
}
